package com.ssm.upms.server.controller.ssm;


import com.jee.ssm.common.json.Tip;
import com.jee.ssm.common.param.ParamMap;
import com.jee.ssm.common.support.Const;
import com.ssm.upms.dao.model.Account;
import com.ssm.upms.rpc.api.ssm.AccountService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 账户 Controller
 * @author GaoXiang
 * @version 1.0
 */
@Controller
@RequestMapping("/account")
public class AccountController extends AdminBaseController<Account> {

    /**
     * 更改信息
     * @param account 用户信息
     * @return 状态
     * @throws Exception 修改失败
     */
    @RequestMapping("/update")
    @ResponseBody
    public Tip update(Account account) throws Exception {
        Account old = accountService.selectById(account.getId());
        old.setPassword(account.getPassword());
        accountService.updateById(old);
        return new Tip();
    }

    @RequestMapping("/edit")
    public String edit(){
        return "manager/account/edit";
    }


    @RequestMapping(value="/checklist")
    @RequiresPermissions("parking:list")
    public String checklist(HttpServletRequest request, ModelMap modelMap) {
        HttpSession session=request.getSession();
        Account account = (Account) session.getAttribute(Const.ACCOUNT);
        ParamMap paramMap=new ParamMap(request);
        if(account.getType()==2){
            paramMap.put("id",  account.getId());
        }
        modelMap.put("pageInfo",accountService.list(paramMap));
        modelMap.putAll(paramMap);
        return "manager/account/chooseAccount";
    }


    /**
     * 禁用管理员登录
     * @param id 管理员id
     * @return 处理状态
     */
    @RequestMapping(value="/forbiddenAccount")
    @ResponseBody
    public Tip forbiddenAccount(@RequestParam String id){
        try {
            accountService.forbiddenAccount(id);
            return new Tip();
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1,e.getMessage());
        }
    }

    /**
     * 允许管理员登录
     * @param id 管理员id
     * @return 处理状态
     */
    @RequestMapping(value="/recoverAccount")
    @ResponseBody
    public Tip recoverAccount(@RequestParam String id){
        try {
            accountService.recoverAccount(id);
            return new Tip();
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1,e.getMessage());
        }
    }



    //----------------------- property ----------------------
    @Resource
    private AccountService accountService;
}
